package android.labis.actividades;

import android.app.ListActivity;
import android.content.Intent;
import android.labis.R;
import android.labis.aplicacion;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ActividadesTab extends ListActivity implements OnItemClickListener  {
	private aplicacion globales;
	private String[] actividades;
	private String[] ids;			

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		globales = (aplicacion) getApplication();
		
		try {
			String[] result = globales.getConexion().GetActividades(globales.getIdcurso());
	    	String rest = ""; 
	    	ids = new String[result.length / 2];
	    	actividades = new String[result.length / 2];
	    	for (int i = 0; i < result.length; i+=2) {
	    		rest += "Id: [" + result[i] + "]\nCurso: [" + result[i+1] + "]\n";
	    		ids[i/2] = result[i];
	    		actividades[i/2] = result[i+1];
	    	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, actividades));

		  ListView lv = getListView();
		  lv.setTextFilterEnabled(true);

		  lv.setOnItemClickListener(this);
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		 globales.setIdactividad(ids[position]);
		  globales.setNombreactividad(actividades[position]);
		  
	      Toast.makeText(getApplicationContext(), getString(R.string.accesing_to)+ " " + getString(R.string.activity) +": " + actividades[position],
		          Toast.LENGTH_SHORT).show();
		
		
		Intent intent = new Intent(this, AlumnosTab.class);  
		startActivity(intent);
		
	}
}
